package com.company;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import lombok.Data;
import lombok.Getter;

import java.util.Date;

/**
 * @date : 2022-11-21 下午5:19
 */

public class P029_JsonFormatEnum {

    public static void main(String[] args) throws JsonProcessingException {
        Person person = new Person();
        person.setId("1234");
        person.setDate(new Date());
        person.setPersonEnum(PersonEnum.PERSON_2);

        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(objectMapper.writeValueAsString(person));

        ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
        System.out.println(objectWriter.writeValueAsString(person));

        String json = JSON.toJSONString(person, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteNullListAsEmpty);
        System.out.println(json);
    }

    @Data
    public static class Person {

        @JsonIgnore
        private String id;

        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        private Date date;

        private PersonEnum personEnum;
    }


    @Getter
    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public enum PersonEnum {

        PERSON_1(1, "张三", 18), PERSON_2(2, "李四", 20), PERSON_3(3, "王五", 31),
        ;

        @JsonIgnore
        private final int id;
        private final String name;
        private final int age;

        PersonEnum(int id, String name, int age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }
    }
}
